<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\ProductRepo;
use App\Repos\ShopRepo;
use App\Repos\OrderPointRepo;
use App\Models\CarBrand;
use App\Models\Cate;


class ProductController extends Controller
{
    public function index(Request $request)
    {
    	$where = [];
    	$data = $request->except(['token','_method','page']);
        $where = array_where($data, function ($value, $key) {
            return $where[$key] = $value;
        });
        //dd($where);
    	$list = ProductRepo::getListPages($where);
    	$shops = ShopRepo::get();
         $cates  = Cate::get();
    	return view('admin.product.index',compact('list','shops','where','cates'));
    }

    public function add()
    {
        $brands = CarBrand::orderBy('is_rec','desc')->get();
        $cates  = Cate::get();
        return view('admin.product.add',compact('brands','cates'));
    }

    public function edit(Request $request)
    {
        $brands = CarBrand::orderBy('is_rec','desc')->get();
        $cates  = Cate::get();
        $id = $request->product_id;
        $data = ProductRepo::find($id);
        return view('admin.product.edit',compact('data','brands','cates'));
    }

    public function save(Request $request)
    {
        $rs['status'] = false;
        $data = $request->except(['_token']);
       
        $flag = ProductRepo::create($data);
        if($flag['status'] == false) {
            $rs['msg'] = $flag['msg'];
            return back()->with('rs',$rs);
        } 
        $rs['status'] = true;
        $rs['msg'] = '操作成功';
        return redirect('zadmin/product')->with('rs',$rs);


    }

    /**
     * 商品评价
     */
    public function point(Request $request)
    {
        $where = [];
        $where['product_id'] = (int)$request->product_id;
        $list = OrderPointRepo::getByProduct($where);
        return view('admin.product.point',compact('list','where'));
    }

    public function saleStatus(Request $request)
    {
        $id = $request->id;
        $data = $request->except(['_token']);
        $data['admin'] =1;
        $flag = ProductRepo::ChangeStatus($id,$data);
        return back();
    }
}
